﻿using Canteen.Core.Models;
using Canteen.Core.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;

namespace Canteen.Core.WebApp.API.Controllers
{
    [Route("admin/user")]
    [ApiController, Authorize(Roles = "admin")]
    public class AdminUserController : ControllerBase
    {
        #region 构造函数
        private readonly IUserService userService;
        
        public AdminUserController(IUserService userService)
        {
            this.userService = userService ?? throw new ArgumentNullException(nameof(userService));
        }
        #endregion

        // GET: /admin/user/pagelist
        [HttpGet("PageList")]
        public async Task<HttpResult<PageList<User>>> GetPageList([FromQuery] UserPageParameters parameters)
        {
            return HttpResult<PageList<User>>.Success(await userService.GetPageList(parameters));
        }
        // GET: /admin/user/recharge/pagelist
        [HttpGet("Recharge/PageList")]
        public async Task<HttpResult<dynamic>> GetRechargePageList([FromQuery] ShopOrderPageParameters parameters)
        {
            return HttpResult<dynamic>.Success(await userService.GetRechargePageList(parameters));
        }
        // 充值
        //POST /admin/user/recharge
        [HttpPost("Recharge")]
        public async Task<HttpResult<decimal>> PostRecharge([FromBody] RechargeRecordDTO rechargeRecord)
        {
            return HttpResult<decimal>.Success(await userService.RechargeWallet(rechargeRecord));
        }
        // GET /admin/user/5
        [HttpGet("{id}")]
        public async Task<HttpResult<User>> Get(int id)
        {
            return HttpResult<User>.Success(await userService.SelectUserById(id));
        }
        // PUT /admin/user
        [HttpPut]
        public async Task<HttpResult<User>> Put([FromBody] User user)
        {
            return HttpResult<User>.Success(await userService.UpdateUser(user));
        }
        // DELETE /admin/user/5
        [HttpDelete("{id}")]
        public async Task<HttpResult<bool>> Delete(int id)
        {
            return HttpResult<bool>.Success(await userService.DeleteUser(id));
        }
    }
}